const fridge = {
	async getAll() {
		await uni.$http.get("/refrigerator").then(({
			data: res
		}) => {
			if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
			if (res.data.length == 0) {
				uni.$store.commit('fridge/updateFridgeList', res.data)
				throw new Error('你还没绑定冰箱!')
			}
			res.data.forEach(x => {
				x.backgroundUrl = uni.$staticResourcePath + x.backgroundUrl
			})
			uni.$store.commit('fridge/updateFridgeList', res.data.reverse())
		}).then(() => {
			// 获取冰箱列表后获取新的默认冰箱的物品列表
			uni.$api.object.getAll()
			this.getStructure()
			uni.$api.object.getAllCateAndObj()
		}).catch((err) => {
			uni.$showMsg(err.message)
		})
	},

	async getStructure() {
		const {
			data: res
		} = await uni.$http.get("/refrigerator/getCurrentRefrigeratorStructure")
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$store.commit('fridge/updateFridgeStructure', res.data)
	},

	async getAllRecord(fid) {
		const {
			data: res
		} = await uni.$http.get("/record/getAllRecord?fid=" + fid)
		if (res.code !== uni.$code.successCode) {
			uni.$showMsg(res.msg)
			return []
		}
		res.data.forEach(x => {
			x.record_list.forEach(y => {
				y.img = uni.$staticResourcePath + y.objectImgPath
				y.op_msg = y.objectName + '-来自-' + y.operatorName
				y.time = new Date(y.operateTime).getMonth() + 1 + '月' + new Date(y.operateTime)
					.getDate() + '日'
				y.op_num = (y.operatorType == '1' ? '-' : '+') + y.operateNum
				y.unit = y.unit || ''
			})
		})
		return res.data
	},

	async getMembersByFid(fid) {
		const {
			data: res
		} = await uni.$http.get("/refrigerator/getUserByFid?fid=" + fid)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		return res.data
	},

	/**
	 * 获取冰箱二维码 V6
	 * @param {Object} fid
	 */
	async getQrCode(fid) {
		const {
			data: res
		} = await uni.$http.get("/refrigerator/getQrCode?fid=" + fid)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		return res.data
	},

	/**
	 * 解绑冰箱
	 * @param {Object} fid
	 */
	async delete(fid) {
		const {
			data: res
		} = await uni.$http.delete('/refrigerator/' + fid)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		// 解绑冰箱后，物品列表要清空
		uni.$store.commit('object/updateObjList', [])
		uni.$showMsg(res.msg)
		this.getAll()
	},

	async save(obj) {
		const {
			data: res
		} = await uni.$http.post("/refrigerator", obj)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		this.getAll()
	},

	/**
	 * 手机号邀请fid&phone
	 * @param {Object} obj
	 */
	async invite(obj) {
		const {
			data: res
		} = await uni.$http.post("/refrigerator/invite", obj)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
	},

	/**
	 * 通过fid加入冰箱组 V6
	 * @param {Object} fid
	 */
	async join(fid) {
		await uni.$http.get("/refrigerator/participateFridge?fid=" + fid).then(({
			data: res
		}) => {
			if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
			uni.$showMsg("你已成功加入冰箱")
			this.getAll()
		}).catch((err) => {
			console.error(err);
			uni.$showMsg("通过fid加入冰箱接口异常");
		})
	},

	async remove(uid, fid) {
		let isSuccess = false
		const {
			data: res
		} = await uni.$http.delete(`/refrigerator/remove?target_uid=${uid}&fid=${fid}`)
		if (res.code !== uni.$code.successCode) {
			uni.$showMsg(res.msg)
		} else {
			isSuccess = true
		}
		return new Promise((resolve, reject) => {
			if (isSuccess)
				resolve("已将该用户移出冰箱组")
			else reject("移出失败")
		})

	},

	async update(obj) {
		const {
			data: res
		} = await uni.$http.put("/refrigerator", obj)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		this.getAll()
	},

	async updateDefault(fid) {
		const {
			data: res
		} = await uni.$http.put('/refrigerator/setDefaultFridge/' + fid)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		this.getAll()
	},
}

export default fridge